package com.night.icm.service.base.service;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.night.icm.common.exception.NoSuchDataException;
import com.night.icm.common.jpa.PageHelper;
import com.night.icm.common.service.PageDataUtil;
import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.ConfigurationInfoDTO;
import com.night.icm.persistence.base.entity.ConfigurationInfo;
import com.night.icm.persistence.base.repository.ConfigurationInfoRepository;
import com.night.icm.service.base.mapper.ConfigurationInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * ConfigurationInfoServiceImpl
 *
 * @author Liao ZhiYong
 * @date 2020/1/24
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ConfigurationInfoServiceImpl implements ConfigurationInfoService {

    private final ConfigurationInfoRepository configurationInfoRepository;
    private final ConfigurationInfoMapper configurationInfoMapper;

    @Autowired
    public ConfigurationInfoServiceImpl(ConfigurationInfoRepository configurationInfoRepository, ConfigurationInfoMapper configurationInfoMapper) {
        this.configurationInfoRepository = configurationInfoRepository;
        this.configurationInfoMapper = configurationInfoMapper;
    }

    /**
     * 分页查询全局参数配置信息
     *
     * @param page      页号，从0开始
     * @param size      每页记录条数
     * @param sort      排序字段，例如：字段1,asc,字段2,desc
     * @param name      名称
     * @param available 是否可用
     * @return 全局参数配置信息
     */
    @Override
    public PageDataDTO<ConfigurationInfoDTO> findOnePage(int page, int size, String sort, String name, Boolean available) {
        PredicateBuilder<ConfigurationInfo> pb = Specifications.<ConfigurationInfo>and()
                .like(StringUtils.isNotBlank(name), "name", "%" + name + "%")
                .eq(available != null, "available", available);
        Page<ConfigurationInfo> entitys = this.configurationInfoRepository.findAll(pb.build(), PageHelper.generatePageRequest(page, size, sort));
        List<ConfigurationInfoDTO> listDTOS = this.configurationInfoMapper.toList(entitys.getContent());
        return PageDataUtil.toPageData(entitys, listDTOS);
    }

    /**
     * 通过id查询全局参数配置信息
     *
     * @param id 全局参数配置Id
     * @return 全局参数配置信息
     */
    @Override
    public ConfigurationInfoDTO findById(String id) {
        Optional<ConfigurationInfo> optional = this.configurationInfoRepository.findById(Long.valueOf(id));
        if (!optional.isPresent()) {
            throw new NoSuchDataException(id);
        }
        ConfigurationInfo configurationInfo = optional.get();
        return this.configurationInfoMapper.toDTO(configurationInfo);
    }

    /**
     * 查找所有全局参数配置信息
     *
     * @return 全局参数配置信息
     */
    @Override
    public List<ConfigurationInfoDTO> findAll() {
        List<ConfigurationInfo> all = this.configurationInfoRepository.findAll();
        return this.configurationInfoMapper.toList(all);
    }

    /**
     * 添加全局参数配置信息
     *
     * @param configurationInfoDTO 全局参数配置信息
     * @return 全局参数配置信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ConfigurationInfoDTO create(ConfigurationInfoDTO configurationInfoDTO) {
        ConfigurationInfo configurationInfo = this.configurationInfoMapper.toEntity(configurationInfoDTO);
        ConfigurationInfo save = this.configurationInfoRepository.save(configurationInfo);
        return this.configurationInfoMapper.toDTO(save);
    }

    /**
     * 根据id删除全局参数配置
     *
     * @param id 全局参数配置Id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        this.configurationInfoRepository.deleteById(Long.valueOf(id));
    }

    /**
     * 更新全局参数配置信息
     *
     * @param id                   全局参数配置id
     * @param configurationInfoDTO 全局参数配置信息
     * @return 更新全局参数配置信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ConfigurationInfoDTO update(String id, ConfigurationInfoDTO configurationInfoDTO) {
        Optional<ConfigurationInfo> optional = this.configurationInfoRepository.findById(Long.valueOf(id));
        if (!optional.isPresent()) {
            throw new NoSuchDataException(id);
        }
        ConfigurationInfo configurationInfo = optional.get();
        this.configurationInfoMapper.updateEntity(configurationInfoDTO, configurationInfo);
        this.configurationInfoRepository.save(configurationInfo);
        return this.configurationInfoMapper.toDTO(configurationInfo);
    }

    /**
     * 检查配置名称唯一性, true为已存在，false为不存在
     *
     * @param name 配置名称
     * @return 检查配置名称唯一性
     */
    @Override
    public SimpleDataDTO<Boolean> checkConfigurationName(String name) {
        SimpleDataDTO<Boolean> simpleDataDTO = new SimpleDataDTO<>();
        Long configurationInfoId = this.configurationInfoRepository.findConfigurationInfoByName(name);
        simpleDataDTO.setData(configurationInfoId != null);
        return simpleDataDTO;
    }
}